package org.shareworth.online_life.client.mvc;

import org.shareworth.online_life.client.module.MyDesktop;
import org.shareworth.online_life.client.module.login.LoginDialog;

import com.extjs.gxt.desktop.client.StartMenu;
import com.extjs.gxt.desktop.client.TaskBar;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;

public class AppView extends View {

	private TaskBar taskBar;
	private StartMenu startMenu;
	private MyDesktop desktop;
	public static String DESKTOP = "desktop";

	public AppView(Controller controller) {
		super(controller);

	}

	@Override
	protected void initialize() {
		desktop = new MyDesktop();
		startMenu = desktop.getStartMenu();
		taskBar = desktop.getTaskBar();

		Registry.register(DESKTOP, desktop);
		// Window window = new Window();
		// window.setSize(200, 200);
		// desktop.addWindow(window);
		// window.setMaximizable(true);
		// window.setMinimizable(true);
		// window.setHeading("imdddd");
		// window.show();
		// Registry.register(DESKTOP, desktop);
		// / Registry.register(TASKBAR, taskBar);
		// Registry.register(START_MENU, startMenu);

		// shortcutGroupList=new ArrayList<ShortcutGroup>();
		// Registry.register(SHORTCUT_GROUP, shortcutGroupList);

		// 用户选择树
		// UserCheckboxWindow window=new UserCheckboxWindow();
		// Registry.register(USER_CHECKBOX,window);
	}

	@Override
	protected void handleEvent(AppEvent event) {

		if (AppEvents.INIT == event.getType()) {

			initUI();

		} else if (AppEvents.LOGIN == event.getType())

			new LoginDialog().show();

	}

	private void initUI() {

		MessageBox wait = MessageBox.wait(null, "登录成功", "正在初始化");

		MenuItem shutdown = new MenuItem("关机",
				new SelectionListener<MenuEvent>() {
					public void componentSelected(MenuEvent ce) {
						MessageBox.confirm("关机", "确定关闭系统吗",
								new Listener<MessageBoxEvent>() {
									public void handleEvent(MessageBoxEvent be) {
										if (be.getButtonClicked().getText()
												.equals("Yes")) {
											close();
										}

									}
								});
					}
				});
		// shutdown.setIcon(commonIcons.lock16());
		MenuItem logout = new MenuItem("注销",
				new SelectionListener<MenuEvent>() {
					public void componentSelected(MenuEvent ce) {
						MessageBox.confirm("注销", "确定注销系统吗",
								new Listener<MessageBoxEvent>() {
									public void handleEvent(MessageBoxEvent be) {
										if (be.getButtonClicked().getText()
												.equals("Yes")) {
											com.google.gwt.user.client.Window.Location
													.reload();
										}
									}
								});

					}
				});
		MenuItem lock = new MenuItem("锁机", new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				// LockDialog.get().lock();
			}
		});
		// lock.setIcon(commonIcons.lock16());

		MenuItem setting = new MenuItem("设置",
				new SelectionListener<MenuEvent>() {
					public void componentSelected(MenuEvent ce) {
						// Preferences.get().show();
					}
				});
		// setting.setIcon(commonIcons.config16());

		startMenu.addTool(setting);
		startMenu.addToolSeperator();
		startMenu.addTool(lock);
		startMenu.addToolSeperator();
		startMenu.addTool(logout);
		startMenu.addTool(shutdown);

		// 桌面右键菜单
		Menu desktopMenu = new Menu();
		MenuItem about = new MenuItem("关于", new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				// About.get().show();
			}
		});
		MenuItem info = new MenuItem("用户信息",
				new SelectionListener<MenuEvent>() {
					public void componentSelected(MenuEvent ce) {
						// PersonalInfo.get().show();
					}
				});
		MenuItem settings = new MenuItem("设置",
				new SelectionListener<MenuEvent>() {
					public void componentSelected(MenuEvent ce) {
						// Preferences.get().show();
					}
				});
		// settings.setIcon(commonIcons.config16());

		desktopMenu.add(settings);
		desktopMenu.add(info);
		desktopMenu.add(new SeparatorMenuItem());
		desktopMenu.add(about);
		desktop.getDesktop().setContextMenu(desktopMenu);

		wait.close();
	}

	private native void close()/*-{
												$wnd.close();
											}-*/;
}
